package ch.manage.movie.business;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;
 
public class MovieScanner {
	
	public static List<Movie> scan(File folder) {
		List<Movie> movieList = new ArrayList<Movie>();
		getFiles(folder, movieList);
		return movieList;
	}
 
    private static void getFiles(File folder, List<Movie> list) {
        folder.setReadOnly();
		FilenameFilter fileFilter = new FilenameFilter() {
			
			@Override
			public boolean accept(File f, String name) {
				if(f.isDirectory() || name.contains(".mkv") || name.contains(".mov")){
					return true;
				}
				return false;
			}
		};
		
		File[] files = folder.listFiles(fileFilter);
		
        
        for(int j = 0; j < files.length; j++) {
            if(!files[j].isDirectory()){
            	Movie movie = new Movie();
            	movie.setPath(files[j]);
            	String movieTitle = files[j].getName();
            	try{
            		movieTitle = movieTitle.substring(0, movieTitle.lastIndexOf("."));
            	}
            	catch (Exception e){
            		System.out.println();
            	}
            	movie.setTitle(movieTitle);
                list.add(movie);
            }else{
            	getFiles(files[j], list);            	
            }
        }
        folder.setWritable(true);
    }
}